package com.company.cloud.mall.modules.mgr.controller;

import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.mall.modules.app.controller.req.OrderByProductSingleSaveParam;
import com.company.cloud.mall.modules.base.entity.OrderInfo;
import com.company.cloud.mall.modules.utils.ShopOrderConvertUtils;
import com.company.cloud.user.modules.api.response.UserFull;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.YNEnum;
import com.company.cloud.common.log.annotation.SysLog;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.EnumUtils;
import com.company.cloud.mall.modules.api.response.OrderModel;
import com.company.cloud.mall.modules.app.controller.req.OrderTicketCodeCheckParam;
import com.company.cloud.mall.modules.app.service.ShopOrderService;
import com.company.cloud.mall.modules.mgr.controller.req.OrderMgrCheckParam;
import com.company.cloud.mall.modules.mgr.controller.req.OrderMgrQuery4PageParam;
import com.company.cloud.mall.modules.mgr.controller.req.OrderSendParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;

/**
 * 订单管理
 */
@Authc
@RestController
@RequestMapping("/mgr/mall/order")
@Api(tags = "Shop_Mgr_订单管理")
public class OrderMgrController {

    @Autowired
    private ShopOrderService shopOrderService;

    @Autowired
    private UserRemoteService userRemoteService;

    @PostMapping("/query")
    @ApiOperation(value = "订单查询", response = BaseResult.class)
    public BaseResult query(@RequestBody OrderMgrQuery4PageParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        if (!"PTC".equals(userInfo.getCompanyNo())) {
            if (YNEnum.YES.getCode().equals(params.getIsAgent())) {
                params.setCompanyNo(userInfo.getCompanyNo());
            } else {
                params.setMainCompanyNo(userInfo.getCompanyNo());
                params.setIsAgent(null);
            }
        }
        params.setBusiness(true);
        params.setUser(true);
        params.setItem(true);
        IPage<OrderModel> data = shopOrderService.queryOrders(params, userInfo);
        return BaseResult.ok(data);
    }

    @PostMapping("/check")
    @SysLog("订单审核")
    @ApiOperation(value = "订单审核", response = BaseResult.class)
    @StaffLogc(title = "订单审核", action = "订单审核", actionAdo = "订单")
    public BaseResult check(@RequestBody OrderMgrCheckParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        shopOrderService.check(params.getSn(), EnumUtils.getBaseCodeEnum(YNEnum.class, params.getStatus()), userInfo);
        return BaseResult.ok();
    }

    @PostMapping("/send")
    @SysLog("订单发货")
    @ApiOperation(value = "订单发货", response = BaseResult.class)
    @StaffLogc(title = "订单发货", action = "订单发货", actionAdo = "订单")
    public BaseResult send(@RequestBody OrderSendParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        shopOrderService.send(params, userInfo);
        return BaseResult.ok();
    }

    @PostMapping("/checkTicketCode")
    @ApiOperation(value = "核销码核销", response = BaseResult.class)
    @StaffLogc(title = "核销码核销", action = "核销码核销", actionAdo = "订单")
    public BaseResult checkTicketCode(@RequestBody OrderTicketCodeCheckParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        shopOrderService.checkTicket(params, userInfo);
        return BaseResult.ok();
    }

    @GetMapping("/get")
    @SysLog("订单明细")
    @ApiOperation(value = "订单明细", response = BaseResult.class)
    public BaseResult get(@RequestParam("sn") String sn) {
        OrderModel data = shopOrderService.getOrder(sn, true, true, true, true, true);
        return BaseResult.ok(data);
    }

}
